package logic

import (
	"context"

	"group/internal/svc"
	"group/pb/group"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMemberListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMemberListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMemberListLogic {
	return &GetMemberListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetMemberList 群聊成员列表
func (l *GetMemberListLogic) GetMemberList(in *group.MemberListRequest) (*group.MemberListResponse, error) {
	total, err := l.svcCtx.GroupMemberExtendModel.Count(l.ctx, in.GroupId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.GroupMemberExtendModel.FindList(l.ctx, in.GroupId, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}

	var data []*group.MemberInfo
	for _, item := range list {
		v := &group.MemberInfo{
			Id:       item.Id,
			UserId:   item.UserId,
			GroupId:  item.GroupId,
			Nick:     item.Nick,
			Role:     item.Role,
			Status:   item.Status,
			JoinTime: item.JoinTime.Unix(),
			CreateAt: item.CreatedAt.Unix(),
			UpdateAt: item.UpdatedAt.Unix(),
		}
		if item.QuitTime.Valid {
			v.QuitTime = item.QuitTime.Time.Unix()
		}
		data = append(data, v)
	}
	return &group.MemberListResponse{Total: total, Data: data}, nil
}
